package servlet;

import dao.UserDao;
import model.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(urlPatterns = "/ModifyUser.do")
public class ModifyServlet extends HttpServlet {
    private UserDao userDao;

    public ModifyServlet() {
        this.userDao = new UserDao();
    }


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String idParam = req.getParameter("id");
        if (idParam == null || idParam.isEmpty()) {
            resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "User ID is required");
            return;
        }

        try {
            int userId = Integer.parseInt(idParam);
            User user = userDao.getUserById(userId);

            if (user != null) {
                HttpSession session = req.getSession();
                session.setAttribute("user", user);
                resp.sendRedirect(req.getContextPath() + "/admin/modifyUser.jsp");
            } else {
                resp.sendError(HttpServletResponse.SC_NOT_FOUND, "User not found");
            }
        } catch (NumberFormatException e) {
            resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "Invalid User ID format");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");

        String idParam = req.getParameter("id");
        if (idParam == null || idParam.isEmpty()) {
            resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "User ID is required");
            return;
        }

        try {
            int userId = Integer.parseInt(idParam);
            String username = req.getParameter("username");
            String gender = req.getParameter("gender");
            String profession = req.getParameter("profession");
            String favor = req.getParameter("favor");
            String description = req.getParameter("description");
            int state = Integer.parseInt(req.getParameter("state"));
            int role = Integer.parseInt(req.getParameter("role"));

            User user = new User();
            user.setId(userId);
            user.setUsername(username);
            user.setGender(gender);
            user.setProfession(profession);
            user.setFavor(favor);
            user.setDescription(description);
            user.setState(state);
            user.setRole(role);

            boolean isUpdated = userDao.updateUser(user);

            if (isUpdated) {
                resp.sendRedirect(req.getContextPath() + "/admin/usermanager.jsp");
            } else {
                resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "User update failed");
            }
        } catch (NumberFormatException e) {
            resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "Invalid input format");
        }
    }
}
